package com.offcn.utils;

import java.util.List;

public class PageTool {
    //总数据条数
    private int totalCount;
    //当前页码
    private int currenPage;
    //总页数
    private int pageCount;
    //上一页页码
    private int lastPage;
    //下一页页码
    private int nextPage;
    //起始下标
    private int startIndex;
    /**
     * 每一页数据条数
     */
    private int pageSize;
    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public PageTool() {
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getCurrenPage() {
        return currenPage;
    }

    public void setCurrenPage(int currenPage) {
        this.currenPage = currenPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getLastPage() {
        return lastPage;
    }

    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    /**
     * 构造方法只需要传递 总数据量 、当前页码、每页总条数其余都可以算出
     */
    public PageTool(int totalCount, String currenPage, int pageSize) {
        //初始化当前总数居量
        this.totalCount = totalCount;
        //初始化每页总条数
        this.pageSize = pageSize;
        //初始化当前页码
        installCurrenPage(currenPage);
        //初始化总页数
        installPageCount();
        //出书画上一页页码
        installLastPage();
        //初始化下一页页码
        installNextPage();
        //初始化起始下标
        installStartIndex();
    }
    //初始化当前页码
    private  void  installCurrenPage(String currenPage){
        if(currenPage != null){
            this.currenPage= Integer.parseInt(currenPage);
        }else {
            this.currenPage=1;
        }
    }
    /**
     * 初始化总页数
     */
    private void installPageCount(){
        this.pageCount=totalCount/pageSize+(totalCount%pageSize == 0?0:1);
    }
    /**
     * 初始化上一页页码
     */
    private  void  installLastPage(){
        if (currenPage != 1){
        this.lastPage=currenPage-1;
        }else {
            lastPage=1;
        }
    }
    /**
     * 初始化下一页页码
     */
    private  void installNextPage(){
        if (currenPage == pageCount){
            this.nextPage=pageCount;
        }else {
            this.nextPage=currenPage+1;
        }
    }
    /**
     * 初始化起始下标
     * */
    private void installStartIndex(){
        this.startIndex=(currenPage-1)*pageSize;
    }

    @Override
    public String toString() {
        return "PageTool{" +
                "总数据量=" + totalCount +
                ", 当前页码=" + currenPage +
                ", 总页数=" + pageCount +
                ", 上一页页码=" + lastPage +
                ", 下一页页码=" + nextPage +
                ", 起始下标=" + startIndex +
                ", 每页数据量=" + pageSize +
                '}';
    }
}
